//
//  ChosseWordViewController.m
//  Learn
//
//  Created by apple on 2024/3/31.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "ChosseWordViewController.h"
#import "BeiDanCiRequest.h"

#import "HMSegmentedControl.h"
#import "CihuiView.h"

#import "ReciteWordPlanViewController.h"
@interface ChosseWordViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) HMSegmentedControl    * segmentedControl;//
@property (nonatomic, strong) UIView    * indicatorView;

@property (nonatomic, strong) UIScrollView    * myScrollView;//

@property (nonatomic, assign) BOOL            isClickedSegmented;       //
@property (nonatomic, assign) NSInteger            startPage;


/* <#描述#> */
@property (nonatomic, strong) CihuiView    * allView;
@property (nonatomic, strong) CihuiView    * chuZhongView;
@property (nonatomic, strong) CihuiView    * xiaoXueView;


@end

@implementation ChosseWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"选择词汇";
    [self.view addSubview:self.segmentedControl];
    [self.view addSubview:self.myScrollView];
    [self requestCiHuiList];
//    [self requestJiaoCaiTypeList];
}
- (HMSegmentedControl *)segmentedControl {
    if (!_segmentedControl) {
        _segmentedControl = [[HMSegmentedControl alloc] initWithFrame:CGRectMake(0, NavigationBarHeight, SCREEN_W, 50)];
        _segmentedControl.sectionTitles = @[@"全部",@"初中",@"小学"];
        _segmentedControl.selectedSegmentIndex = 0;
        _segmentedControl.backgroundColor = UIColor.whiteColor;
        _segmentedControl.titleTextAttributes = @{NSForegroundColorAttributeName : UIColor.blackColor, NSFontAttributeName : PingFangMediumFont(18)};
        _segmentedControl.selectedTitleTextAttributes = @{NSForegroundColorAttributeName : COLOR_TOPIC_SELECT, NSFontAttributeName : PingFangMediumFont(18)};
        _segmentedControl.selectionIndicatorHeight = 0;
        _segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
        WS(WeakSelf)
        [_segmentedControl setIndexChangeBlock:^(NSUInteger index) {
            WeakSelf.isClickedSegmented = YES;
            WeakSelf.indicatorView.centerX = WeakSelf.segmentedControl.width/6 + index *WeakSelf.segmentedControl.width/3;
            
            [WeakSelf.myScrollView scrollRectToVisible:CGRectMake(SCREEN_W * index, 0, SCREEN_W, CGRectGetHeight(WeakSelf.myScrollView.frame)) animated:YES];
        }];
        [_segmentedControl addSubview:self.indicatorView];
    }
    return _segmentedControl;
}
- (UIView *)indicatorView {
    if(!_indicatorView){
        _indicatorView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 28, 3)];
        _indicatorView.centerX = self.segmentedControl.width/6;
        _indicatorView.bottom = self.segmentedControl.height - 10;
        _indicatorView.backgroundColor = COLOR_TOPIC_SELECT;
        _indicatorView.layer.cornerRadius = 1.5;
        _indicatorView.layer.masksToBounds = YES;
    }
    return _indicatorView;
}
- (UIScrollView *)myScrollView{
    if (!_myScrollView)
    {
        _myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.segmentedControl.bottom, SCREEN_W,SCREEN_H - self.segmentedControl.bottom)];
        _myScrollView.delegate = self;
        _myScrollView.backgroundColor = UIColor.whiteColor;
        _myScrollView.pagingEnabled = YES;
        _myScrollView.contentSize = CGSizeMake(SCREEN_W * 3, CGRectGetHeight(_myScrollView.frame));
       
        [_myScrollView addSubview:self.allView];
        [_myScrollView addSubview:self.chuZhongView];
        [_myScrollView addSubview:self.xiaoXueView];

    }
    return _myScrollView;
}

- (CihuiView *)allView {
    if(!_allView){
        _allView = [[CihuiView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, CGRectGetHeight(_myScrollView.frame))];
        WS(WeakSelf)
        _allView.CiHuiItemClicked = ^(CiHuiModel * _Nonnull model) {
            [WeakSelf pushNextVCWithModel:model];

        };
    }
    return _allView;
}

- (CihuiView *)chuZhongView {
    if(!_chuZhongView){
        _chuZhongView = [[CihuiView alloc]initWithFrame:CGRectMake(self.allView.right, 0, SCREEN_W, CGRectGetHeight(_myScrollView.frame))];
        WS(WeakSelf)
        _chuZhongView.CiHuiItemClicked = ^(CiHuiModel * _Nonnull model) {
            [WeakSelf pushNextVCWithModel:model];

        };
    }
    return _chuZhongView;
}
- (CihuiView *)xiaoXueView {
    if(!_xiaoXueView){
        _xiaoXueView = [[CihuiView alloc]initWithFrame:CGRectMake(self.chuZhongView.right, 0, SCREEN_W, CGRectGetHeight(_myScrollView.frame))];
        WS(WeakSelf)
        _xiaoXueView.CiHuiItemClicked = ^(CiHuiModel * _Nonnull model) {
            [WeakSelf pushNextVCWithModel:model];

        };
    }
    return _xiaoXueView;
}

- (void)pushNextVCWithModel:(CiHuiModel *)model {
    ReciteWordPlanViewController *planVC = [[ReciteWordPlanViewController alloc]init];
    planVC.model = model;
    [self.navigationController pushViewController:planVC animated:YES];
}


#pragma mark - ----------------------- 代理 -----------------------
#pragma mark 解决Segmented的滑块快速滑动时的延迟，同时把点击滑块的情况排除在外
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (!self.isClickedSegmented)
    {
        CGFloat pageWidth = scrollView.frame.size.width;
        NSInteger tmpPage = scrollView.contentOffset.x / pageWidth;
        float tmpPage2 = scrollView.contentOffset.x / pageWidth;
        NSInteger page = tmpPage2-tmpPage>=0.5 ? tmpPage+1 : tmpPage;
        if (_startPage != page)
        {
            _startPage = page;
            self.indicatorView.centerX = self.segmentedControl.width/6 + page *self.segmentedControl.width/3;
            
            [self.segmentedControl setSelectedSegmentIndex:page animated:YES];
        }
    }
}
    //
#pragma mark 页面滚动，同时调起Segmented的滑块滑动起来等
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pageWidth = scrollView.frame.size.width;
    NSInteger page = scrollView.contentOffset.x / pageWidth;
    _startPage = page;
   
    self.isClickedSegmented = NO;
    self.indicatorView.centerX = self.segmentedControl.width/6 + page *self.segmentedControl.width/3;
    
    [self.segmentedControl setSelectedSegmentIndex:page animated:YES];
}

#pragma mark   数据请求
- (void)requestCiHuiList {
    WS(WeakSelf)
    [BeiDanCiRequest request_getCihuiListWithVersionName:@"" result:^(NSMutableArray * _Nonnull allList, NSMutableArray * _Nonnull chuList, NSMutableArray * _Nonnull xiaoList) {
        [WeakSelf.allView updateList:allList];
        [WeakSelf.chuZhongView updateList:chuList];
        [WeakSelf.xiaoXueView updateList:xiaoList];
        [WeakSelf requestJiaoCaiTypeList];
    }];
    
    

}
- (void)requestJiaoCaiTypeList {
    WS(WeakSelf)

    [BeiDanCiRequest request_getJiaoCaiListResult:^(NSMutableArray * _Nonnull chuList, NSMutableArray * _Nonnull xiaoList) {
        [WeakSelf.chuZhongView updateHeaderList:chuList];
        [WeakSelf.xiaoXueView updateHeaderList:xiaoList];
    }];
}
@end
